package com.sgb.kjwl.view.ui.adapter.search;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Paint;
import android.text.TextUtils;
import android.view.View;

import com.sgb.alimaplibrary.util.ToastUtil;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemMaterialGoodsBinding;
import com.sgb.kjwl.model.entity.user.GoodsListEntity;
import com.sgb.kjwl.utils.LocationUtil;
import com.sgb.kjwl.utils.NumberFormatUtils;
import com.sgb.kjwl.utils.PointUtils;
import com.sgb.kjwl.view.ui.activity.manage.WebsiteInfoActivity;
import com.sgb.kjwl.view.ui.adapter.enterprise.TextViewBindingAdapter;
import com.swgk.core.helper.OnMultiClickListener;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.text.DecimalFormat;
import java.util.List;

/**
 * @author yanghao
 */
public class MaterialAdapter extends BaseRecycleAdapter<GoodsListEntity> {

    private DecimalFormat df = new DecimalFormat("#.00");
    private final String SELF_EMPLOYED = "1";
    private final int PROMOTE_GOODS = 2;
    private boolean isMap;
    private ActionCallBack callBack;

    public void setCallBack(ActionCallBack callBack) {
        this.callBack = callBack;
    }

    public MaterialAdapter(Context context, List<GoodsListEntity> data, boolean isMap) {
        super(context, R.layout.item_material_goods, data);
        this.isMap = isMap;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void convert(ItemViewHolder holder, final GoodsListEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        ItemMaterialGoodsBinding itemMaterialGoodsBinding = (ItemMaterialGoodsBinding) holder.getmBinding();
        if (isMap) {
            itemMaterialGoodsBinding.actionContainer.setVisibility(View.VISIBLE);
            if (entity.isST()) {
                itemMaterialGoodsBinding.tvAddShoppingCar.setVisibility(View.INVISIBLE);
                itemMaterialGoodsBinding.tvPurchase.setVisibility(View.INVISIBLE);
            } else {
                itemMaterialGoodsBinding.tvAddShoppingCar.setVisibility(View.VISIBLE);
                itemMaterialGoodsBinding.tvPurchase.setVisibility(View.VISIBLE);
            }
        } else {
            itemMaterialGoodsBinding.actionContainer.setVisibility(View.GONE);
        }

        itemMaterialGoodsBinding.contentView.setOnClickListener(v -> {
            if (TextUtils.isEmpty(entity.getCompanyNo())) {
                ToastUtil.show(holder.itemView.getContext(), "企业店铺No不能为空");
                return;
            }
            WebsiteInfoActivity.start(holder.itemView.getContext(), entity.getCompanyNo(), WebsiteInfoActivity.WEBSITE_FOR_COMPANY, 0, 0);
        });

        if (entity.getPromoteStatus() == PROMOTE_GOODS&&!TextUtils.isEmpty(entity.getVipPrice())&&Double.valueOf(entity.getVipPrice())>0) {
            itemMaterialGoodsBinding.imgCx.setVisibility(View.VISIBLE);
            itemMaterialGoodsBinding.tvLastPrice.setVisibility(View.VISIBLE);
            itemMaterialGoodsBinding.tvLastPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            itemMaterialGoodsBinding.tvLastPrice.setText("¥" + df.format(Double.valueOf(entity.getOriginPrice())) + "/" + entity.getUnit());
            if (Double.valueOf(entity.getPromotePrice())<Double.valueOf(entity.getVipPrice())){
                itemMaterialGoodsBinding.tvSpec.setText(TextViewBindingAdapter.setMoney(PointUtils.price(Double.parseDouble(entity.getPromotePrice()))));
                itemMaterialGoodsBinding.cuPrice.setTextColor(context.getResources().getColor(R.color.new_org2));
                itemMaterialGoodsBinding.tvSpec.setTextColor(context.getResources().getColor(R.color.new_org2));
                itemMaterialGoodsBinding.priceView.setTextColor(context.getResources().getColor(R.color.new_org2));
                itemMaterialGoodsBinding.iconVip.setVisibility(View.GONE);
            }else {
                itemMaterialGoodsBinding.tvSpec.setText(TextViewBindingAdapter.setMoney(PointUtils.price(Double.parseDouble(entity.getVipPrice()))));
                itemMaterialGoodsBinding.cuPrice.setTextColor(context.getResources().getColor(R.color.colorF7AB01));
                itemMaterialGoodsBinding.tvSpec.setTextColor(context.getResources().getColor(R.color.colorF7AB01));
                itemMaterialGoodsBinding.priceView.setTextColor(context.getResources().getColor(R.color.colorF7AB01));
                itemMaterialGoodsBinding.iconVip.setVisibility(View.VISIBLE);
            }
        } else if (entity.getPromoteStatus()== PROMOTE_GOODS){
            itemMaterialGoodsBinding.imgCx.setVisibility(View.VISIBLE);
            itemMaterialGoodsBinding.tvSpec.setText(TextViewBindingAdapter.setMoney(PointUtils.price(Double.parseDouble(entity.getPromotePrice()))));
            itemMaterialGoodsBinding.cuPrice.setTextColor(context.getResources().getColor(R.color.new_org2));
            itemMaterialGoodsBinding.tvSpec.setTextColor(context.getResources().getColor(R.color.new_org2));
            itemMaterialGoodsBinding.priceView.setTextColor(context.getResources().getColor(R.color.new_org2));
            itemMaterialGoodsBinding.tvLastPrice.setVisibility(View.VISIBLE);
            itemMaterialGoodsBinding.tvLastPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            itemMaterialGoodsBinding.tvLastPrice.setText("¥" + df.format(Double.valueOf(entity.getOriginPrice())) + "/" + entity.getUnit());
            itemMaterialGoodsBinding.iconVip.setVisibility(View.GONE);
        }else if (!TextUtils.isEmpty(entity.getVipPrice())&&Double.valueOf(entity.getVipPrice())>0){
            itemMaterialGoodsBinding.imgCx.setVisibility(View.GONE);
            itemMaterialGoodsBinding.tvSpec.setText(TextViewBindingAdapter.setMoney(PointUtils.price(Double.parseDouble(entity.getVipPrice()))));
            itemMaterialGoodsBinding.cuPrice.setTextColor(context.getResources().getColor(R.color.colorF7AB01));
            itemMaterialGoodsBinding.tvSpec.setTextColor(context.getResources().getColor(R.color.colorF7AB01));
            itemMaterialGoodsBinding.priceView.setTextColor(context.getResources().getColor(R.color.colorF7AB01));
            itemMaterialGoodsBinding.iconVip.setVisibility(View.VISIBLE);
            itemMaterialGoodsBinding.tvLastPrice.setVisibility(View.VISIBLE);
            itemMaterialGoodsBinding.tvLastPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            itemMaterialGoodsBinding.tvLastPrice.setText("¥" + df.format(Double.valueOf(entity.getOriginPrice())) + "/" + entity.getUnit());
        } else {
            itemMaterialGoodsBinding.imgCx.setVisibility(View.GONE);
            itemMaterialGoodsBinding.tvLastPrice.setVisibility(View.GONE);
            itemMaterialGoodsBinding.tvSpec.setText(TextViewBindingAdapter.setMoney(PointUtils.price(Double.valueOf(entity.getOriginPrice()))));
            itemMaterialGoodsBinding.cuPrice.setTextColor(context.getResources().getColor(R.color.new_org));
            itemMaterialGoodsBinding.tvSpec.setTextColor(context.getResources().getColor(R.color.new_org));
            itemMaterialGoodsBinding.priceView.setTextColor(context.getResources().getColor(R.color.new_org));
            itemMaterialGoodsBinding.iconVip.setVisibility(View.GONE);
        }

        if (LocationUtil.isOpenLocation(context) && LocationUtil.currentLocationIsValid()) {
            itemMaterialGoodsBinding.distanceContainer.setVisibility(View.VISIBLE);
            itemMaterialGoodsBinding.tvDistance.setText(NumberFormatUtils.baseFormatAmt(Double.valueOf(entity.getDistance()),1,false) + "km");
        } else {
            itemMaterialGoodsBinding.distanceContainer.setVisibility(View.GONE);
        }

        if (SELF_EMPLOYED.equals(entity.getSelfSupportTag())) {
            itemMaterialGoodsBinding.labelContainer.setVisibility(View.VISIBLE);
            itemMaterialGoodsBinding.imageZiying.setVisibility(View.VISIBLE);
        } else {
            itemMaterialGoodsBinding.labelContainer.setVisibility(View.GONE);
            itemMaterialGoodsBinding.imageZiying.setVisibility(View.GONE);
        }

        if (entity.isSellGoodGoods()) {
            if (entity.getRanking() == 1) {
                itemMaterialGoodsBinding.ivRanking.setImageResource(R.mipmap.icon_sell_good_number1);
            } else if (entity.getRanking() == 2) {
                itemMaterialGoodsBinding.ivRanking.setImageResource(R.mipmap.icon_sell_good_number2);
            } else if (entity.getRanking() == 3) {
                itemMaterialGoodsBinding.ivRanking.setImageResource(R.mipmap.icon_sell_good_number3);
            } else {
                itemMaterialGoodsBinding.ivRanking.setImageDrawable(null);
            }
            if (entity.getBrandName() == null || "".equals(entity.getBrandName())) {
                itemMaterialGoodsBinding.tvBrandName.setVisibility(View.GONE);
                if (SELF_EMPLOYED.equals(entity.getSelfSupportTag())) {
                    itemMaterialGoodsBinding.labelContainer.setVisibility(View.VISIBLE);
                } else {
                    itemMaterialGoodsBinding.labelContainer.setVisibility(View.GONE);
                }
            } else {
                itemMaterialGoodsBinding.labelContainer.setVisibility(View.VISIBLE);
                itemMaterialGoodsBinding.tvBrandName.setVisibility(View.VISIBLE);
                itemMaterialGoodsBinding.tvBrandName.setText(entity.getBrandName());
            }
        }
        itemMaterialGoodsBinding.tvStorageName.setText("仓储:" + entity.getStorageName());

        itemMaterialGoodsBinding.tvPhone.setOnClickListener(v -> {
            if (callBack != null) {
                callBack.onClickedCallBack(1, entity);
            }
        });
        itemMaterialGoodsBinding.tvXunjia.setOnClickListener(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                if (callBack != null) {
                    callBack.onClickedCallBack(2, entity);
                }
            }
        });
        itemMaterialGoodsBinding.tvAddShoppingCar.setOnClickListener(v -> {
            if (callBack != null) {
                callBack.onClickedCallBack(3, entity);
            }
        });
        itemMaterialGoodsBinding.tvPurchase.setOnClickListener(v -> {
            if (callBack != null) {
                callBack.onClickedCallBack(4, entity);
            }
        });
    }

    public interface ActionCallBack {
        /**
         * 接口回调
         *
         * @param type
         * @param entity
         */
        void onClickedCallBack(int type, GoodsListEntity entity);
    }

}
